from django.shortcuts import render
from django.http.request import HttpRequest
from django.http.response import HttpResponse


# Create your views here.
def index(request):

    return HttpResponse('ok')
'''
# 方法1：
from book.models import BookInfo
# 创建一个实例对象
book = BookInfo()
# 调用实例对象的属性
book.name = 'django'
book.readcount = 10
book.commentcount = 100
book.pub_date = '2000-2-2'
# 手动调用save方法
# save 方法是 父类 ORM中已经封装好的
book.save()
'''
'''
方法2：
objects 我们可以理解为代理人，也是ORM的父类已经封装好的
new_book=BookInfo.objects.create(
    name='django',
    pub_date='2020-1-1',
    readcount=10,
    commentcount=100
    )
更新数据：
from book.models import BookInfo
book=BookInfo.objects.get(id=1)
book.name='射雕英雄后传'
book.pub_date='2020-1-1'
book.save()

BookInfo.objects.filter(id=1).update(
    name='~~~~射雕英雄前传',
    pub_date='2020-2-2')
    
#删除
book=BookInfo.objects.get(id=6)
book.delete()

BookInfo.objects.get(id=5).delete()

基本查询
BookInfo.objects.get(id=2)
BookInfo.objects.get(name='雪山飞狐')

捕获异常：
try:
    BookInfo.objects.get(id=5)
except BookInfo.DoesNotExist as e:
    print('没有查询到')
    
BookInfo.objects.all()

count查询数量
BookInfo.objects.all().count()
BookInfo.objects.count()

查询：过滤查询
BookInfo.objects.get(id__exact=2)
book=BookInfo.objects.get(id=2)
list=BookInfo.objects.get(id=2)

BookInfo.objects.filter(name__contains='湖')
BookInfo.objects.filter(name__endswith='部')
BookInfo.objects.filter(name__isnull=True)
BookInfo.objects.filter(id__in=[1,3,5])
BookInfo.objects.filter(id__gt=3)
BookInfo.objects.filter(pub_date__year=1986)
BookInfo.objects.filter(pub_date__gt='1990-1-1')

BookInfo.objects.exclude(id=3)

查询F对象

from django.db.models import F
BookInfo.objects.filter(readcount__gte=F('commentcount'))
BookInfo.objects.filter(id__gt=2).filter(readcount__gt=20)
BookInfo.objects.filter(id__gt=2,readcount__gt=20)

from django.db.models import Q
BookInfo.objects.filter(Q(id__gt=2)&Q(readcount__gt=20))
BookInfo.objects.filter(Q(id__gt=2)|Q(readcount__gt=20))
BookInfo.objects.exclude(id=3)
BookInfo.objects.filter(~Q(id=3))

查询聚合函数
from django.db.models import Max,Min,Count,Sum,Avg
BookInfo.objects.aggregate(Sum('readcount'))

BookInfo.objects.all().order_by('readcount')
BookInfo.objects.all().order_by('-readcount')

关联查询
book.peopleinfo_set.all()
person=PeopleInfo.objects.get(id=6)
person.book
from book.models import PeopleInfo
from book.models import BookInfo
BookInfo.objects.filter(peopleinfo__name='东方不败')
BookInfo.objects.filter(peopleinfo__description__contains='八')
PeopleInfo.objects.filter(book__readcount__gt=30)
PeopleInfo.objects.filter(book__name='天龙八部')
book=BookInfo.objects.all()
book
books=BookInfo.objects.all()
[book.id for book in books]
BookInfo.objects.all()[:2]
BookInfo.objects.all()[0:2]
BookInfo.objects.all()[1:2]
BookInfo.objects.all()[2]


分页

from django.core.paginator import Paginator

books=BookInfo.objects.all()

p=Paginator(books,per_page=2)

page_objects=p.page(1)
p.num_pages
page_objects.object_list
'''
from book.models import BookInfo
from book.serializers import BookinfoSerializers
#创建对象
book = BookInfo.objects.get(id = 2)

#创建序列化器
#serializer(instance = 实例对象)
serializer = BookinfoSerializers(instance=book)

#对象转字典
serializer.data

####################################获取多个对象
from book.models import BookInfo
from book.serializers import BookinfoSerializers

book = BookInfo.objects.all()
#如果传递的是查询结果集,用many = Ture 告知序列化器
s = BookinfoSerializers(book,many=True)

s.data

##############################people
from book.models import PeopleInfo
from book.serializers import PeopleinfoSerializers

people = PeopleInfo.objects.get(id = 6)

s = PeopleinfoSerializers(people)

s.data

####################################反序列化
from book.serializers import BookinfoSerializers

data = {
    # 'id':10,
    'name':'python',
    'pub_date':'2010-1-1',
    'readcount':0,
    'commentcount':0,
  'is_delete':False,
    'people':[{'name':'aaa'}]
}
#Serializer(instance=None, data=empty, **kwarg)
# instance 主要用于 对象转字典
# data 主要用于 字典转对象
s = BookinfoSerializers(instance=None, data=data)

s.is_valid(raise_exception=True)  #精益不知道我们的数据为什么不满足要求 raise_exception 抛出异常
